/**
 * 例 9-5
 * block.c
 * */ 

#include <sys/types.h>
#include <sys/socket.h>
#include <error.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>

void main(int argc, char **argv)
{
   int sock;
   struct sockaddr_in my_addr;
   int asynflag = 1;
   time_t t1, t2;
   if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
   {
      perror("socket create error!\n");
      exit(1);
   }
   my_addr.sin_family = AF_INET;
   my_addr.sin_port = htons(8888);
   if (inet_aton(argv[1], (struct in_addr *)&my_addr.sin_addr.s_addr) == 0)
   {
      printf("%s chage error!\n", argv[1]);
      exit(1);
   }
   t1 = time(NULL);

   ioctl(sock, FIONBIO, &asynflag);

   if (connect(sock, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) < 0)
   {
      if (errno != EINPROGRESS)
      {
         perror("connect error!\n");
         // exit(1);
      }
   }
   t2 = time(NULL);
   printf("connect used time is %d second\n", (int)(t2 - t1));
   close(sock);
}
